package com.rzico.market.plugin.phoneBill;


import com.rzico.util.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class DxPhoneXmlUtil {

    //预下单路径
    public static final String url_order = "http://bapi.juhemall.cn/order.action";
    //订单查询路径
    public static final String url_queryOrder = "http://bapi.juhemall.cn/queryOrder.action";


    //中国移动号码格式验证 手机段：134,135,136,137,138,139,147,150,151,152,157,158,159,178,182,183,184,187,188,1705
    private static final String CHINA_MOBILE_PATTERN = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478]|9[8])\\d{8}$)|(^1703|1705|1706\\d{7}$)";

    //中国电信号码格式验证 手机段： 133,149,153,173,177,180,181,189,199 虚拟营运商1700,1701,1702
    private static final String CHINA_TELECOM_PATTERN = "(^1(33|49|53|73|77|99|8[019])\\d{8}$)|(^1700|1701|1702\\d{7}$)";

    //中国联通号码格式验证 手机段：130,131,132,145,155,156,175,176,185,186 虚拟营运商1704,1707,1708,1709
    private static final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|7[56]|8[56])\\d{8}$)|(^1704|1707|1708|1709\\d{7}$)";

    /**
     * 查询电话属于哪个运营商
     *
     * @param tel 手机号码
     * @return 0：不属于任何一个运营商，1:移动，2：联通，3：电信
     */
    public static int isChinaMobilePhoneNum(String tel) {
        boolean b1 = false;
        b1 = tel == null || tel.trim().equals("") ? false : match(CHINA_MOBILE_PATTERN, tel);
        if (b1) {
            return 1;
        }
        b1 = tel == null || tel.trim().equals("") ? false : match(CHINA_TELECOM_PATTERN, tel);
        if (b1) {
            return 2;
        }
        b1 = tel == null || tel.trim().equals("") ? false : match(CHINA_UNICOM_PATTERN, tel);
        if (b1) {
            return 3;
        }
        return 0;
    }

    /**
     * 匹配函数
     * @param regex
     * @param tel
     * @return
     */
    private static boolean match(String regex, String tel) {
        return Pattern.matches(regex, tel);
    }


    /**
     * xml转map
     * @param xml xml
     * @return map
     */
    public static Map<String,Object> xmlToMap(String xml){
        Map<String, Object> map = new HashMap<>();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            List<Element> list = rootElt.elements();//获取根节点下所有节点
            for (Element element : list) {  //遍历节点
                map.put(element.getName(), element.getText()); //节点的name为map的key，text为map的value
            }
        } catch (Exception e) {
            return null;
        }
        return map;
    }

    /**
     * 订购拼接报文体
     * @param params
     * @return
     */
    public static final String buildOrderReqXml(Map<String,Object> params) {
        StringBuffer sb = new StringBuffer();
        sb.append("<request>").append("\r\n");
        sb.append(buildOrderReqHeaderXml(params));
        sb.append(" <body>").append("\r\n");
        List<Map<String, Object>> infoList = null;
        if (params.containsKey("info")) {
            infoList = (List<Map<String, Object>>) params.get("info");
        }
        if (0 < infoList.size()) {
            for (Map<String, Object> map : infoList) {
                sb.append("  <item>").append("\r\n");
                if (map.containsKey("packCode") && StringUtils.isNotEmpty(map.get("packCode").toString())) {
                    sb.append("    <packCode>").append(map.get("packCode").toString()).append("</packCode>").append("\r\n");
                } else {
                    sb.append("    <packCode></packCode>").append("\r\n");
                }
                if (map.containsKey("mobile") && StringUtils.isNotEmpty(map.get("mobile").toString())) {
                    sb.append("    <mobile>").append(map.get("mobile").toString()).append("</mobile>").append("\r\n");
                } else {
                    sb.append("    <mobile></mobile>").append("\r\n");
                }
                if (map.containsKey("effectType") && StringUtils.isNotEmpty(map.get("effectType").toString())) {
                    sb.append("    <effectType>").append(map.get("effectType").toString()).append("</effectType>").append("\r\n");
                } else {
                    sb.append("    <effectType></effectType>").append("\r\n");
                }
                sb.append("  </item>").append("\r\n");
            }
        }
        sb.append(" </body>").append("\r\n");
        sb.append("</request>").append("\r\n");
        return sb.toString();
    }

    /**
     * 订购拼接报文头
     * @param params
     * @return
     */
    public static final String buildOrderReqHeaderXml(Map<String,Object> params) {
        StringBuffer sb = new StringBuffer();
        sb.append(" <head>").append("\r\n");
        if (params.containsKey("custInteId") && StringUtils.isNotEmpty(params.get("custInteId").toString())) {
            sb.append("    <custInteId>").append(params.get("custInteId").toString()).append("</custInteId>").append("\r\n");
        } else {
            sb.append("    <custInteId></custInteId>").append("\r\n");
        }
        if (params.containsKey("orderId") && StringUtils.isNotEmpty(params.get("orderId").toString())) {
            sb.append("    <orderId>").append(params.get("orderId").toString()).append("</orderId>").append("\r\n");
        } else {
            sb.append("    <orderId></orderId>").append("\r\n");
        }
        if (params.containsKey("orderType") && StringUtils.isNotEmpty(params.get("orderType").toString())) {
            sb.append("    <orderType>").append(params.get("orderType").toString()).append("</orderType>").append("\r\n");
        } else {
            sb.append("    <orderType></orderType>").append("\r\n");
        }
        if (params.containsKey("echo") && StringUtils.isNotEmpty(params.get("echo").toString())) {
            sb.append("    <echo>").append(params.get("echo").toString()).append("</echo>").append("\r\n");
        } else {
            sb.append("    <echo></echo>").append("\r\n");
        }
        if (params.containsKey("timestamp") && StringUtils.isNotEmpty(params.get("timestamp").toString())) {
            sb.append("    <timestamp>").append(params.get("timestamp").toString()).append("</timestamp>").append("\r\n");
        } else {
            sb.append("    <timestamp></timestamp>").append("\r\n");
        }
        if (params.containsKey("version") && StringUtils.isNotEmpty(params.get("version").toString())) {
            sb.append("    <version>").append(params.get("version").toString()).append("</version>").append("\r\n");
        } else {
            sb.append("    <version></version>").append("\r\n");
        }
        if (params.containsKey("chargeSign") && StringUtils.isNotEmpty(params.get("chargeSign").toString())) {
            sb.append("    <chargeSign>").append(params.get("chargeSign").toString()).append("</chargeSign>").append("\r\n");
        } else {
            sb.append("    <chargeSign></chargeSign>").append("\r\n");
        }

        sb.append(" </head>").append("\r\n");
        return sb.toString();
    }

    /**
     * 拼接查询订单体
     * @param params
     * @return
     */
    public static final String buildOrderQueryReqXml(Map<String,Object> params) {
        StringBuffer sb = new StringBuffer();
        sb.append("<request>").append("\r\n");
        sb.append(buildQueryRequestHeaderXml(params));
        sb.append(" <body>").append("\r\n");
        List<Map<String, Object>> infoList = (List<Map<String, Object>>) params.get("info");
        if (0 < infoList.size()) {
            for (Map<String, Object> map : infoList) {
                sb.append("  <item>").append("\r\n");
                if (map.containsKey("orderIds") && StringUtils.isNotEmpty(map.get("orderIds").toString())) {
                    sb.append("    <orderIds>").append(map.get("orderIds").toString()).append("</orderIds>").append("\r\n");
                } else {
                    sb.append("    <orderIds></orderIds>").append("\r\n");
                }
            }
            sb.append("  </item>").append("\r\n");
        }
        sb.append(" </body>").append("\r\n");
        sb.append("</request>").append("\r\n");
        return sb.toString();

    }

    /**
     * 拼接查询订单头
     * @param params
     * @return
     */
    public static final String buildQueryRequestHeaderXml(Map<String,Object> params) {
        StringBuffer sb = new StringBuffer();
        sb.append(" <head>").append("\r\n");
        if (params.containsKey("custInteId") && StringUtils.isNotEmpty(params.get("custInteId").toString())) {
            sb.append("    <custInteId>").append(params.get("custInteId").toString()).append("</custInteId>").append("\r\n");
        } else {
            sb.append("    <custInteId></custInteId>").append("\r\n");
        }
        if (params.containsKey("echo") && StringUtils.isNotEmpty(params.get("echo").toString())) {
            sb.append("    <echo>").append(params.get("echo").toString()).append("</echo>").append("\r\n");
        } else {
            sb.append("    <echo></echo>").append("\r\n");
        }
        if (params.containsKey("timestamp") && StringUtils.isNotEmpty(params.get("timestamp").toString())) {
            sb.append("    <timestamp>").append(params.get("timestamp").toString()).append("</timestamp>").append("\r\n");
        } else {
            sb.append("    <timestamp></timestamp>").append("\r\n");
        }
        if (params.containsKey("version") && StringUtils.isNotEmpty(params.get("version").toString())) {
            sb.append("    <version>").append(params.get("version").toString()).append("</version>").append("\r\n");
        } else {
            sb.append("    <version></version>").append("\r\n");
        }
        if (params.containsKey("chargeSign") && StringUtils.isNotEmpty(params.get("chargeSign").toString())) {
            sb.append("    <chargeSign>").append(params.get("chargeSign").toString()).append("</chargeSign>").append("\r\n");
        } else {
            sb.append("    <chargeSign></chargeSign>").append("\r\n");
        }
        sb.append(" </head>").append("\r\n");
        return sb.toString();
    }


}
